/*
 * The Open Source Unix Installer
 *
 * The MIT License
 *
 * Copyright 2011-2012 Andrey Pudov.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

/*
 * @author  Andrey Pudov    <andrey@andreypudov.com>
 * @version 0.00.00
 * @name    u_shell.h
 * @date:   Feb 27, 2013
 */

#ifndef INSTALLER_UTIL_U_SHELL_H_
#define INSTALLER_UTIL_U_SHELL_H_

#include <stdio.h>
#include <stdbool.h>

#include "shell.h"

#ifdef __cplusplus
extern "C" {
#endif

bool u_installer_shell_start();                 /* unit entry point */

bool u_installer_shell_new();
bool u_installer_shell_delete();
bool u_installer_shell_execute();

bool u_installer_shell_start() {
    return (u_installer_shell_new()
        && u_installer_shell_delete()
        && u_installer_shell_execute());
}

bool u_installer_shell_new() {
    Shell *shell = installer_shell_new();

    if ((shell == NULL) || (shell->pty != 0)) {
        u_installer_main_print("shell", "new()", false);
        
        return false;
    }
    
    installer_shell_delete(shell);
    
    u_installer_main_print("shell", "new()", true);
    
    return true;
}

bool u_installer_shell_delete() {
    /* unit test is a part of new */
    
    u_installer_main_print("shell", "delete()", true);
    
    return true;
}

bool u_installer_shell_execute() {
    Shell *shell = installer_shell_new();
    
    const char *login    = "root";
    const char *password = "M@rin@2012";
    const char *command  = "echo $(whoami) > /tmp/installer-shell_execute";
    
    const char *command2 = "rm -f /tmp/installer-shell_execute";
    
    FILE *file_pointer = NULL;
    
    int  unit     = 0;
    char line[80] = {0};
    
    unit = installer_shell_execute(shell, login, password, command);
    if ((unit != AUTH_SUCCESS) || (errno != 0)) {
        u_installer_main_print("shell", "execute(1)", false);
        
        return false;
    }
    
    file_pointer = fopen("/tmp/installer-shell_execute", "r");
    if (file_pointer == NULL) {
        u_installer_main_print("shell", "execute(2)", false);
        
        return false;
    }
    
    if (fgets(line, 80, file_pointer) == NULL) {
        u_installer_main_print("shell", "execute(3)", false);
        
        return false;
    }
    
    if (strncmp(line, login, 4) != 0) {
        u_installer_main_print("shell", "execute(4)", false);
        
        return false;
    }
    
    fclose(file_pointer);
    
    unit = installer_shell_execute(shell, login, password, command2);
    if (unit != AUTH_SUCCESS) {
        u_installer_main_print("shell", "execute(5)", false);
                
        return false;
    }
    
    u_installer_main_print("shell", "execute()", true);
        
    return true;
}

#ifdef __cplusplus
}
#endif

#endif /* INSTALLER_UTIL_U_SHELL_H_ */ 